<?php
/**
 * Created by: i@fedax.cn
 * Date: 2018/7/16 22:40
 * 靓号管理
 */

namespace app\admin\controller;

use app\admin\model\NiceId as NiceIdModel;

class PrettySign extends Index
{
    /**
     * @return \think\response\View
     * 靓号列表
     */
    public function index()
    {
        return view();
    }

    /**
     * @return \think\response\Json
     * 靓号数据渲染
     */
    public function indexData()
    {
        $niceIdModel = new NiceIdModel();
        $data = $niceIdModel->getDataList();
        return json($data);
    }

    /**
     * @return \think\response\Redirect
     * 靓号数据添加
     */
    public function add()
    {
        $niceIdModel = new NiceIdModel();

        $newData['nice_num'] = input('nice_num');
        $newData['status'] = input('status');

        $re = $niceIdModel->where('nice_num','=',$newData['nice_num'])->find();
        if($re)   $this->error('靓号已存在');

        $res = $niceIdModel->save($newData);
        if($res)  $this->success(lang('add').lang('success'));
        else  $this->error(lang('add').lang('false'));

        return redirect('/Miss');
    }

    /**
     * @return \think\response\Redirect
     * 靓号上架下架
     */
    public function changeStatus()
    {
        $niceIdModel = new NiceIdModel();
        if(request()->isPost()){
            $niceIdDetail  = $niceIdModel->where('id='.input('id'))->find();
            $newData['status'] = $niceIdDetail['status'] === 1?0:1;
            try{
                $niceIdModel->save($newData,['id'=>input('id')]);
            }catch(\Exception $e){
                $this->error(lang('edit').lang('false').$e->getMessage());
            }
            $this->success(lang('edit').lang('success'));
        }
        return redirect('/Miss');
    }

}